<template>
  <div>
    <!-- 折扣策略查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhDiscountStrategyQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" @click="exportHandler">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, exportExcel ,exporturl} from '@/api/omFinance/PriceDiscountManage/DiscountStrategyQuery';
import { exportExcelFile } from '@/utils/index';
import SonModel from './SonModel';
import {getNotPage } from '@/api/omFinance';

export default {
  name: 'DiscountStrategyQuery',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    this.conf = { edit: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      exportLoading: false,
      visible: false,
      handleType: '',
      drawerTitle: ''
    };
  },
  components: {
    SonModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    // 订单细分
     async getNotPage(orderType) {
      let res = await getNotPage({ orderType: orderType, isAvailable: 10041001 });
      if (res.resultCode == 200) {
        this.topFilterList.find(x => x.fieldName == 'orderTypeSub').itemList = res.data.map(x => ({ text: x.subName, value: x.subId }));
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    detailHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitCloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    // 导出
    async exportHandler() {
        let json = Object.assign({}, { CODE: 'CODE_00000230' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;
      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '折扣策略查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '折扣策略名称',
          fieldName: 'policyName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '启用标志',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003')
        },
        {
          type: 'RANGE_DATE',
          label: '生效日期',
          fieldName: 'beginStartDate|endStartDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'RANGE_DATE',
          label: '失效日期',
          fieldName: 'beginEndDate|endEndDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '审批状态',
          fieldName: 'checkStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6029')
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
         {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('6006'),
          change: val => {
            this.topFilterList.find(x => x.fieldName == 'orderTypeSub').initialValue = '';
            if (val) {
              this.getNotPage(val);
              return;
            }
            this.topFilterList.find(x => x.fieldName == 'orderTypeSub').itemList = [];
          }
        },
        {
          type: 'SELECT',
          label: '订单类型细分',
          fieldName: 'orderTypeSub',
          placeholder: '请选择',
          itemList: []
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '折扣策略名称',
          dataIndex: 'policyName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '生效日期',
          dataIndex: 'startDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '失效日期',
          dataIndex: 'endDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '启用标志',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          render: props => {
            return <span>{this.createDictText(props.row.status, '1003')}</span>;
          }
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '提交人',
          dataIndex: 'reportPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提交时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        {
          title: '审批状态',
          dataIndex: 'checkStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6029'),
          render: props => {
            return <span>{this.createDictText(props.row.checkStatus, '6029')}</span>;
          }
        },
        {
          title: '审批人',
          dataIndex: 'checkPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '审批时间',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range'
        },
        {
          title: '审批意见',
          dataIndex: 'auditRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
